﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeFirstDomain.ModelContext;
using Framework.Services;
using CodeFirstDomain.IService;
using Framework.Infrastructure;
using Framework.UnitOfWork;
using CodeFirstDomain.Model;

namespace CodeFirstDomain.Service
{
    class AppService : ApplicationService, IAppService
    {
        public AppService(IUnitOfWork uow) : base(uow) { }

        #region IAppService Members

        public void Test()
        {
            DoTest();
            UnitOfWork.Commit();
        }

        internal void DoTest()
        {
            var userRepository = GetRepository<User>();
            var userID = new Guid("b3c32fc5-33fa-4c74-86bc-19baba8b756f");
            var user = userRepository.GetByKey(userID);
            if (user == null)
            {

                user = new User { ID = userID, Address = new Address(DateTime.Now.ToShortDateString(), "building1") };
               
                userRepository.Add(user);
                UnitOfWork.Commit();
            }
            //userRepository.Remove(user);
            //RepositoryContext.Commit();
            //var mblogs = user.SpecialMBlogs;
            //var list = mblogs.ToList();
            //user.MBlogs.Add(new MBlog { ID = Guid.NewGuid(), Body = "blog1", CreateTime = DateTime.Now, Status = EnableStatus.Normal });
            //user.MBlogs.Add(new MBlog { ID = Guid.NewGuid(), Body = "blog2", CreateTime = DateTime.Now, Status = EnableStatus.Normal });
            //user.MBlogs.Add(new MBlog { ID = Guid.NewGuid(), Body = "blog3", CreateTime = DateTime.Now, Status = EnableStatus.Normal });
            user.Address = new Address(DateTime.Now.ToShortDateString(), "building1");
            var query = user.GetQueryable(u => u.MBlogs);
            List<MBlog> mblogs = query.ToList();

            var mblog = new MBlog { Author = user, ID = Guid.NewGuid(), Body = "blog1", CreateTime = DateTime.Now, Status = EnableStatus.Normal };
            mblog.MarkAsAdded();
          //  mblog.Added();
            user.CollectionLoad(u => u.MBlogs);
          
            UnitOfWork.Commit();
        }

        #endregion
        
    }
}
